package com.ordering.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5
{
    public static String crypt(String str) throws NoSuchAlgorithmException
    {
        if (str == null || str.length() == 0)
        {
            throw new IllegalArgumentException("String to encript cannot be null or zero length");
        }
        StringBuffer hexString = new StringBuffer();

        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] hash = md.digest();

            for (int i = 0; i < hash.length; i++)
            {
                if ((0xff & hash[i]) < 0x10)
                {
                    hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
                }
                else
                {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        }
        catch (NoSuchAlgorithmException e)
        {
            throw e;
        }
        return hexString.toString();
    }
    public static void main(String[] args)
    {
        try
        {
            System.out.println(crypt("1"));
            System.out.println(crypt(crypt("111111")));
            if("9db06bcff9248837f86d1a6bcf41c9e7".equals(crypt(crypt("111111")))){
            	System.out.println("yes");
            }
        }
        catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
